package com.wj.controller;

import com.wj.domain.dto.AddShelfDTO;
import com.wj.domain.param.BookParam;
import com.wj.domain.vo.*;
import com.wj.service.BookService;
import com.wj.service.ChapterContentService;
import com.wj.service.ChapterService;
import com.wj.tool.common.bean.ChapterReturnVO;
import com.wj.tool.common.bean.restful.RestfulBody;
import com.wj.tool.common.exception.SelfException;
import com.wj.tool.common.utils.StringUtils;
import com.wj.tool.common.utils.paging.PageUtil;
import com.wj.tool.common.utils.paging.QueryParameters;
import com.wj.tool.common.utils.paging.QueryResults;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/book")
@RestfulBody
@Api(tags = "小程序接口-小说管理")
public class ApiBookController {

    @Resource
    private BookService bookService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private ChapterContentService chapterContentService;

    @ApiOperation("小说类型列表")
    @GetMapping("/type/list")
    public List<BookTypeVO> getTypeList(){
        return bookService.getTypeList();
    }

    /**
     * 分页搜索
     */
    @PostMapping("/search/page")
    @ApiOperation("分页查询小说列表")
    public QueryResults<BookVO> searchByPage(@RequestBody QueryParameters<BookParam> parameters) {
        return bookService.getBookPage(parameters);
    }

    /**
     * 书详情
     * @param bookId
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation("书的详情")
    public BookVO getBookInfo(@RequestParam("bookId") Long bookId){
        return bookService.getBookById(bookId);
    }

    @GetMapping("/getchapterlist")
    @ApiOperation("获取该书的所有章节列表--数量多会很慢")
    public List<ChapterVO> getBookChapterList(@RequestParam("bookId") Long bookId) {
        return chapterService.queryListByBookId(bookId);
    }

    @GetMapping("/chapter/group/list/{param}")
    @ApiOperation("获取该书的所有章节组列表")
    public Map<String, Object> getChapterGroupList(@PathVariable("param") String param){
        Map<String, Object> map = new HashMap<>();
        Long bookId = null;
        int pageNo = 1;
        if (StringUtils.isNotEmpty(param) && param.contains("_")){
            bookId = Long.valueOf(param.split("_")[0]);
            pageNo = Integer.valueOf(param.split("_")[1]);
        } else if (StringUtils.isNotEmpty(param) && !param.contains("_")) {
            bookId = Long.valueOf(param);
        } else {
            throw new SelfException("传参有误");
        }
        int pageSize = 20;
        List<ChapterVO> chapterList = chapterService.queryListByBookId(bookId);
        QueryResults<ChapterVO> queryResults = PageUtil.listToPage(pageNo, pageSize, chapterList);
        List<ChapterVO> chapterVOList = queryResults.getResults();

        int chapterNumber = chapterList.size();
        List<ChapterReturnVO> chapterReturnVOList = new ArrayList<>();
        int p = 1;
        for (int i=1;i <= chapterNumber;i++){
            if (i > 0 && i % pageSize == 0){
                ChapterReturnVO returnVO = new ChapterReturnVO();
                String title = "";
                int fromIndex = i - 19;
                title += "第" + fromIndex + "章-第"+ i + "章";
                returnVO.setPageNumber(p);
                returnVO.setTitle(title);
                chapterReturnVOList.add(returnVO);
                p++;
            }
            if (i == chapterNumber){
                ChapterReturnVO returnVO = new ChapterReturnVO();
                String title = "";
                int fromIndex = (chapterNumber / pageSize) * pageSize + 1;
                title += "第" + fromIndex + "章-第"+ i + "章";
                returnVO.setPageNumber(p);
                returnVO.setTitle(title);
                chapterReturnVOList.add(returnVO);
                p++;
            }

        }
        map.put("chapterReturnVOList", chapterReturnVOList);
        map.put("chapterList", chapterVOList);
        return map;
    }

    @GetMapping("/getchaptercontent/{bookId}/{sort}")
    @ApiOperation("书的某章节内容")
    public ChapterContentVO getBookChapterContent(@PathVariable("bookId") Long bookId, @PathVariable("sort") int sort) {
        ChapterVO chapterVO = chapterService.netChapter(bookId, sort);
        ChapterContentVO chapterContentVO = chapterContentService.getById(chapterVO.getContentId());
        ChapterUpAndDownVO chapterUpAndDownVO = chapterService.getUpAndDown(bookId, sort);
        if (chapterContentVO != null){
            chapterContentVO.setChapterTitle(chapterVO.getChapterTitle());
            chapterContentVO.setChapterUpAndDownVO(chapterUpAndDownVO);
        }
        return chapterContentVO;
    }

    @GetMapping("/my/shelf/list")
    public List<BookShelfVO> getMyBookShelf(){
        return bookService.getMyBookShelfList();
    }

    @GetMapping("/remove/shelf")
    public boolean removeBookShelf(@RequestParam("bookId") Long bookId){
        return bookService.removeBookShelf(bookId);
    }

    @PostMapping("/add/shelf")
    public boolean addBookShelf(@RequestBody @Valid AddShelfDTO shelfDTO){
        return bookService.addBookShelf(shelfDTO);
    }

}